<?php

//Commnons include
require_once( "../common/function.php" );

class FieldCreationException extends Exception{
	
}

class FieldLoadingException extends Exception{
	
}

class FieldStoreException extends Exception{
	
}

class FieldDeleteException extends Exception{
	
}

class Field{
	
	//Attributes
    private $id;
    
	private $name;
	
	private $label;
	
	private $form_display;
	
	private $form_validation;
	
	//Constructor
	public function __construct( $id = null , $name = null, $label = null ){
		
		if( $id !== null ){
		
			$field_type_id = mysql_real_escape_string( $id );
		
			$mysql_query_load_field_type = " SELECT name, label FROM ".CONST_FIELD_TYPES_TABLE." WHERE id = {$field_type_id} ";
			
			$res = mysql_query( $mysql_query_load_field_type );
			
			if( $res === false ){
			
				Throw new FieldCreationException();
				
			}
			
			if( mysql_num_rows() != 1 ){
			
				Throw new FieldCreationException();
				
			}
			
		}
		
		else if( $name !== null and $label !== null ){
			
			$field_type_name = mysql_real_escape_string( $name );
			
			$field_type_label = mysql_real_escape_string( $label );
			
			$mysql_query_create_field_type = " INSERT INTO ".CONST_FIELD_TYPES_TABLE." VALUES( '{$field_type_name}' , '{$field_type_label}' ) ";
			
			$res = mysql_query( $mysql_query_create_field_type );
			
			if( $res === false ){
			
				Throw new FieldLoadingException();
				
			}		
			
			//Cannot retrieve the generated id, false error, 0 not generated
			if( !( $insertId = mysql_insert_id() ) ){
				
				Throw new FieldLoadingException();
				
			}	
			
			$this->id = $insertId;			
			
		}				
	
	}
	
	//Getters
	public function getName(){
		
		return $this->name;
		
	}
	
	public function getLabel(){
		
		return $this->label;
		
	}
	
	public function getForm_display(){
		
		return $this->form_display;
		
	}
	
	public function getForm_validation(){
		
		return $this->form_validation;
		
	}
	
	//Setters
	public function setName( $name ){
		
		//TODO $name validation
		
		$this->name = $name;
		
	}
	
	public function setLabel( $label ){
		
		//TODO $label validation
		
		$this->label = $label;
		
	}
	
	public function setForm_display( $functionCode ){
		
		//TODO $functionCode validation
		
		$this->form_display = $functionCode;
		
	}
	
	public function setForm_validation( $functionCode ){
		
		//TODO $functionCode validation
		
		$this->form_validation = $functionCode;
		
	}

	//Update
	public function update(){	
		
			//TODO mysql escape fields
			
			$mysql_query_update_field_type = " UPDATE ".CONST_FIELD_TYPES_TABLE." 
			
												   SET name = '{$this->name}' , label = '{$this->label}' ) 
												   
														WHERE id = {$this->id} ";
			
			$res = mysql_query( $mysql_query_update_field_type );
			
			if( $res === false ){
			
				Throw new FieldStoreException();
				
			}		
		
	}
	
	//Delete
	public function delete(){
		
		    //TODO mysql escape fields
			
			$mysql_query_delete_field_type = " DELETE FROM".CONST_FIELD_TYPES_TABLE." 
												   											   
			         								WHERE id = {$this->id} ";
			
			$res = mysql_query( $mysql_query_delete_field_type );
			
			if( $res === false ){
			
				Throw new FieldDeleteException();
				
			}		
		
	}
		
}

?>
